package br.pesquisa.lsystem.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.Set;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import br.pesquisa.lsystem.Celula;
import br.pesquisa.lsystem.Gramatica;
import br.pesquisa.lsystem.Celula.Sentido;

@SuppressWarnings("serial")
public class AlfabetoFrame extends JInternalFrame {

	private Gramatica<Celula> gramatica;
	private MainFrame mainFrame;
	private JList listAlfabeto;
	private JComboBox comboLetras;
	private JComboBox comboSentido;

	public AlfabetoFrame() {
		super("Alfabeto");
	}

	public Gramatica<Celula> getGramatica() {
		return gramatica;
	}

	public void setGramatica(Gramatica<Celula> gramatica) {
		this.gramatica = gramatica;
	}

	public MainFrame getMainFrame() {
		return mainFrame;
	}

	public void setMainFrame(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
	}

	public void init() {
		listAlfabeto = new JList();
		populateList();

        JScrollPane listScroller = new JScrollPane(listAlfabeto);
        listScroller.setAlignmentX(LEFT_ALIGNMENT);

		JPanel listPanel = new JPanel();
		listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.PAGE_AXIS));
		JLabel label = new JLabel("Alfabeto");
		label.setLabelFor(listAlfabeto);
		listPanel.add(label);
		listPanel.add(Box.createRigidArea(new Dimension(0,5)));
		listPanel.add(listScroller);
		listPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

		comboLetras = new JComboBox();
		for(char c = 'A'; c <= 'Z'; c++) {
			comboLetras.addItem(String.valueOf(c));
		}

		comboSentido = new JComboBox();
		comboSentido.addItem("-");
		comboSentido.addItem(Sentido.direita);
		comboSentido.addItem(Sentido.esquerda);

		JButton buttonAdicionar = new JButton("Adicionar");
		buttonAdicionar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String letra = comboLetras.getSelectedItem().toString();
				String sentido = comboSentido.getSelectedItem().toString();
				Celula celula = new Celula(letra);
				if(!sentido.equals("-")) {
					celula.setSentido(Sentido.valueOf(sentido));
				}
				if(!getGramatica().getAlfabeto().contem(celula)) {
					getGramatica().getAlfabeto().adicionar(celula);
					populateList();
					mainFrame.atualizarAlfabeto();
				}
			}
		});

		JPanel fieldsPanel = new JPanel();
		fieldsPanel.setLayout(new BoxLayout(fieldsPanel, BoxLayout.LINE_AXIS));
		fieldsPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		fieldsPanel.add(new JLabel("Letra: "));
		fieldsPanel.add(comboLetras);
		fieldsPanel.add(Box.createRigidArea(new Dimension(10, 0)));
		fieldsPanel.add(new JLabel("Sentido: "));
		fieldsPanel.add(comboSentido);
		fieldsPanel.add(Box.createRigidArea(new Dimension(10, 0)));
		fieldsPanel.add(buttonAdicionar);

		add(fieldsPanel, BorderLayout.NORTH);
		add(listPanel, BorderLayout.CENTER);
	}

	private void populateList() {
		Set<Celula> elementos = getGramatica().getAlfabeto().getElementos();
		Vector<String> celulas = new Vector<String>(elementos.size());
		for(Celula celula: elementos) {
			celulas.add(celula.getLetra() + (celula.getSentido() == null? "": " - " + celula.getSentido()));
		}

		Collections.sort(celulas);
		listAlfabeto.setListData(celulas);
	}
}
